//
//  CWHomeOriginalView.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/26.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import SnapKit

/**
 原创微博的视图
 */

class CWHomeOriginalView: UIView {
    //原创微博配图的底部约束
    var originalBottomConstraint: Constraint?
    
    var originalViewModel: CWStatusViewModel? {
        didSet{
            originalBottomConstraint?.deactivate()
            //为控件赋值,首先得做判断
            guard let currentViewModel = originalViewModel else {
                return
            }
            //代码执行到此说明originalViewModel一定有值
            //加载用户头像
            if let imgURL = currentViewModel.statusModel?.user?.profile_image_url{
                userIcon.yy_setImage(with: URL(string: imgURL), placeholder: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = currentViewModel.statusModel?.user?.screen_name
            contentLabel.text = currentViewModel.statusModel?.text
            mbrankImageView.image = currentViewModel.mbrankImage
            verifiedTypeImageView.image = currentViewModel.verifiedTypeImage
            if let currentPicture = currentViewModel.statusModel?.pic_urls,currentPicture.count > 0 {
                //有配图视图
                pictureView.isHidden = false
                pictureView.picURLs = currentPicture
                pictureView.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(self).offset(-homeCellMargin).constraint
                })
            }else {
                //没有配图视图
                pictureView.isHidden = true
                contentLabel.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(self).offset(-homeCellMargin).constraint
                })
            }
        }
    }
    
    //MARK: 懒加载控件
    //配图视图
    fileprivate lazy var pictureView: CWHomePictureView = {
        
        let picture = CWHomePictureView()
        picture.backgroundColor = self.backgroundColor
        return picture
    }()
    
    fileprivate lazy var userIcon: UIImageView = {
        let imgV: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        return imgV
    }()
    
    fileprivate lazy var verifiedTypeImageView: UIImageView = {
        let imgV: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
        return imgV
    }()
    
    fileprivate lazy var mbrankImageView: UIImageView = {
        let imgV: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
        return imgV
    }()
    
    fileprivate lazy var screenNameLabel: UILabel = {
       
        let label: UILabel = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "XXX"
        return label
    }()
    
    fileprivate lazy var timeLabel: UILabel = {
        
        let label: UILabel = UILabel(fontSize: 12, textColor: UIColor.orange)
        label.text = "刚刚"
        return label
    }()
    
    fileprivate lazy var sourceLabel: UILabel = {
        
        let label: UILabel = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        label.text = "iPhone7"
        return label
    }()
    
    fileprivate lazy var contentLabel: UILabel = {
        
        let label: UILabel = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        label.text = "Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/"
        label.numberOfLines = 0
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {

        addSubview(userIcon)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrankImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        userIcon.snp.makeConstraints { (make) in
            
            make.top.leading.equalTo(10)
            make.width.height.equalTo(35)
        }
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userIcon.snp.trailing)
            make.centerY.equalTo(userIcon.snp.bottom)
        }
        screenNameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIcon)
            make.leading.equalTo(userIcon.snp.trailing).offset(homeCellMargin)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(screenNameLabel)
            make.top.equalTo(screenNameLabel.snp.bottom).offset(homeCellMargin)
        }
        sourceLabel.snp.makeConstraints { (make) in
            
            make.bottom.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(homeCellMargin.multiplied(by: 2))
        }
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenNameLabel)
            make.leading.equalTo(screenNameLabel.snp.trailing).offset(homeCellMargin.multiplied(by: 2))
            make.height.width.equalTo(20)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(timeLabel.snp.bottom).offset(homeCellMargin)
            make.leading.equalTo(userIcon)
            make.trailing.equalTo(self).offset(-homeCellMargin)
            //make.bottom.equalTo(self).offset(-homeCellMargin)
        }
        pictureView.snp.makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(homeCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
            originalBottomConstraint = make.bottom.equalTo(self).offset(-homeCellMargin).constraint
        }
    }

}
